import Circle from '../Circle'
import { Collision } from '../Collision'
import { bullet } from '../bullet'
import { Engine } from '../engine'


/**
 * 默认敌人对象
 *
 * @class defaultEnemy
 */
class defaultEnemy {
    constructor() {
        this.index = 0;
        this.obj = new Circle(30);
        this.obj.x = -50;
        this.obj.y = 50;
        this.interval = 0;
        this.directionX = 0;
        this.directionY = 0;
        this.bullets = [];
        this.Health = 100;
        this.die = false;
        this.DrawEvent = Engine;
    }

    /**
     * 敌人被玩家子弹击中事件
     *
     * @memberof defaultEnemy
     */
    chechHit() {
        var color = '#ffffff';
        for (let i = 0; i < this.DrawEvent.playerBullets.length; i++) {
            const element = this.DrawEvent.playerBullets[i];
            let ans = Collision(element, this.obj);
            if (ans) {
                this.DrawEvent.removeObject(element, 'playerBullets');
                this.Health -= element.hit;
                if (this.Health <= 0) {
                    this.die = true;
                }
                color = '#FF0000';
            }
        }
        this.obj.color = color;
    }

    /**
     * 移动算法
     *
     * @param {canvas} c 渲染画布对象
     * @memberof defaultEnemy
     */
    move(c) {
        this.index++;
        if (this.index < 100) {
            var x = (this.DrawEvent.realWidth / 2) / 100 * this.index;
            this.obj.x = x;
        } else if (this.index == 100) {
            this.directionX = 5;
        } else if (this.index > 100 && this.index < 500) {
            // 移动方法
            if (this.obj.x > this.DrawEvent.realWidth) {
                this.directionX = -5
            } else if (this.obj.x < 0) {
                this.directionX = 5
            }
        } else {
            this.directionX = 0;
            this.directionY = 2;
            if (this.obj.y > this.DrawEvent.realHeight + 100) {
                this.die = true;
            }
        }
        this.obj.x += this.directionX;
        this.obj.y += this.directionY;
        // this.obj.draw(c);

        c.translate(this.obj.x, this.obj.y);
        c.beginPath();
        c.arc(0, 0, this.obj.r, 0, Math.PI * 2);
        c.globalAlpha = 1;
        c.strokeStyle = this.obj.color;
        c.lineWidth = 2;
        c.stroke();
        c.restore();

    }
    /**
     * 子弹算法
     *
     * @memberof defaultEnemy
     */
    bulletEvent() {
        this.interval++;
        if (this.interval < 35) {
            if (this.index % 5 == 0) {
                var _bullet = new bullet(3);
                _bullet.x = this.obj.x;
                _bullet.y = this.obj.y;
                this.DrawEvent.addObject(_bullet, 'enemyBullets');
            }
        } else if (this.interval > 50) {
            this.interval = 0;
        };
    }

    /**
     * 渲染事件
     *
     * @param {canvas} c 渲染对象
     * @memberof defaultEnemy
     */
    draw(c) {
        if (!this.die) {
            this.move(c);
            if (this.index > 100) {
                this.bulletEvent();
                this.chechHit();
            }
        } else {
            this.DrawEvent.removeObject(this, 'enemy')
        }
    }
}

export default defaultEnemy
